package com.songshuai.sales.tool.upload;


import com.songshuai.sales.tool.service.SopImgService;
import com.songshuai.webapi.baseapi.dto.feign.response.custminiprogram.UploadImgResDto;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api("文件上传")
@RestController
@RequestMapping("/api/upload/")
public class UploadController {

    public final static String jsonStr = "{\"videoMaxSize\":102400000,\"videoActionName\":\"uploadvideo\",\"fileActionName\":\"uploadfile\",\"fileManagerListPath\":\"/ueditor/jsp/upload/file/\",\"imageCompressBorder\":1600,\"imageManagerAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"imageManagerListPath\":\"/ueditor/jsp/upload/image/\",\"fileMaxSize\":51200000,\"fileManagerAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\",\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\",\".rar\",\".zip\",\".tar\",\".gz\",\".7z\",\".bz2\",\".cab\",\".iso\",\".doc\",\".docx\",\".xls\",\".xlsx\",\".ppt\",\".pptx\",\".pdf\",\".txt\",\".md\",\".xml\"],\"fileManagerActionName\":\"listfile\",\"snapscreenInsertAlign\":\"none\",\"scrawlActionName\":\"uploadscrawl\",\"videoFieldName\":\"upfile\",\"imageCompressEnable\":true,\"videoUrlPrefix\":\"\",\"fileManagerUrlPrefix\":\"\",\"catcherAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"imageManagerActionName\":\"listimage\",\"snapscreenPathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\"scrawlPathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\"scrawlMaxSize\":2048000,\"imageInsertAlign\":\"none\",\"catcherPathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\"catcherMaxSize\":2048000,\"snapscreenUrlPrefix\":\"\",\"imagePathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\"imageManagerUrlPrefix\":\"\",\"scrawlUrlPrefix\":\"\",\"scrawlFieldName\":\"upfile\",\"imageMaxSize\":2048000,\"imageAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"snapscreenActionName\":\"uploadimage\",\"catcherActionName\":\"catchimage\",\"fileFieldName\":\"upfile\",\"fileUrlPrefix\":\"\",\"imageManagerInsertAlign\":\"none\",\"catcherLocalDomain\":[\"127.0.0.1\",\"localhost\",\"img.baidu.com\"],\"filePathFormat\":\"/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}\",\"videoPathFormat\":\"/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}\",\"fileManagerListSize\":20,\"imageActionName\":\"uploadimage\",\"imageFieldName\":\"upfile\",\"imageUrlPrefix\":\"\",\"scrawlInsertAlign\":\"none\",\"fileAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\",\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\",\".rar\",\".zip\",\".tar\",\".gz\",\".7z\",\".bz2\",\".cab\",\".iso\",\".doc\",\".docx\",\".xls\",\".xlsx\",\".ppt\",\".pptx\",\".pdf\",\".txt\",\".md\",\".xml\"],\"catcherUrlPrefix\":\"\",\"imageManagerListSize\":20,\"catcherFieldName\":\"source\",\"videoAllowFiles\":[\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\"]}";

    @Autowired
    private SopImgService sopImgService;

    @ApiOperation(value = "图片上传")
    @RequestMapping("uploadPicture")
    public Object uploadPicture(@RequestParam(name = "file", required = false) MultipartFile file) {

        log.info("file返回结果："+file);
        Map map = new HashMap();
        map.put("url","");
        map.put("origunal","");
        map.put("state","");
        map.put("title","");

        try {
            QueryRes q = sopImgService.uploadPicture(file);
            log.info("返回结果："+q.toString());
            UploadImgResDto o =(UploadImgResDto)q.getResult();

            map.put("url",o.getImgUrl());
            map.put("origunal",file.getOriginalFilename());
            map.put("state","SUCCESS");
            map.put("title",file.getOriginalFilename());


        } catch (Exception ex) {
            log.error("api/upload/uploadPicture", ex);
            map.put("state","ERROR");


        }
        return map;
    }


    /*ueditor*/
    @RequestMapping(value = "/config")
    public Object config(@RequestParam(name = "upfile", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response)  {

        String action =  request.getParameter("action");
        String callback = request.getParameter("callback");

        Map map = new HashMap();
        map.put("url","");
        map.put("origunal","");
        map.put("state","");
        map.put("title","");

        try {

            if("config".equals(action)){
                //response.setContentType("application/json");
                //request.setCharacterEncoding("utf-8");
                //PrintWriter writer = response.getWriter();
                //writer.write(callback+"("+jsonStr+")");
                //writer.flush();
                //writer.close();
                return callback+"("+jsonStr+")";
            }else{
               return this.uploadPicture(file);
            }
        }catch (Exception ex){
            log.error("api/upload/config", ex);
            map.put("state","ERROR");

        }

        return map;

    }


    //获取七牛云token
    @RequestMapping(value = "/getQiNiuToken")
    public Object getQiNiuToken()  {

        Map map = new HashMap();
        map.put("token", "");
        try {
            String token = sopImgService.getQiNiuToken();
            map.put("state", "SUCCESS");
            map.put("token", token);
            return map;
        }catch (Exception ex){
            log.error("api/upload/getQiNiuToken", ex);
            map.put("state", "ERROR");
            return map;
        }
    }


}
